<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px;width: 100%"
  >
    <el-col>
      <!-- 名称应该变成 对应的节点中的name -->
      <span>{{ treeNode.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row
        type="flex"
        justify="end"
      >
        <!-- 两个内容 -->
        <el-col>{{ treeNode.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown @command="handleCommand">
            <span>
              操作<i class="el-icon-arrow-down" />
            </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <slot>
                <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                <el-dropdown-item
                  v-if="!isRoot"
                  command="edit"
                >编辑部门</el-dropdown-item>
                <!-- 方式1 通过dom事件冒泡 -->
                <el-dropdown-item
                  v-if="!isRoot"
                  command="del"
                >删除部门</el-dropdown-item>
              </slot>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartment } from '@/api/department.js'
export default {
  name: 'TreeTools',
  components: {

  },
  props: {
    treeNode: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      // required: true,
      default: false
    }
  },
  data() {
    return {

    }
  },
  created() {

  },
  methods: {
    handleCommand(command) {
      if (command === 'add') {
        // 新增
        this.$emit('addDept', this.treeNode)
      } else if (command === 'edit') {
        // 编辑
        this.$emit('editDept', this.treeNode)
      } else if (command === 'del') {
        // 删除
        // 通过elmecon
        this.$confirm('确认删除', '提示', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async action => {
          await delDepartment(this.treeNode.id)
          // 通过elme
          this.$message({
            message: '删除成功',
            type: 'success',
            showClose: true,
            duration: 3000
          })
          this.$emit('delDept')
        }).catch(() => {

        })
      }
    },
    handleDel() {
      console.log('删除')
    }
  }
}
</script>

<style lang="less" scoped>
</style>
